package com.itouch.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.itouch.bracelet.R;

/**
 * cocoaSJ
 */
public class CircleLayout extends ViewGroup {

	public static final String TAG = "CircleLayout";
	private int width;
	private GestureDetector mDetector;

	public CircleLayout(Context context) {
		super(context);
		init();
	}

	public CircleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public CircleLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public void init() {
//		mDetector = new GestureDetector(CircleLayout.this.getContext(),
//				new GestureListener());
	}

//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		// Let the GestureDetector interpret this event
//		boolean result = mDetector.onTouchEvent(event);
//
//		if (!result) {
//			if (event.getAction() == MotionEvent.ACTION_UP) {
//				result = true;
//			}
//		}
//		return result;
//	}

	double x = 0.0;

	private class GestureListener extends
			GestureDetector.SimpleOnGestureListener {

		// ��������Ļ�ϻ����뿪��Ļʱ����
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {

			Log.e("=============", "onScroll============");

			return true;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			Log.e("=============", "onFling============");
			if (e2.getX()<e1.getX()) {
				temp--;
			} else {
				temp++;
			}
			requestLayout();	
			return true;
		}

		@Override
		public boolean onDown(MotionEvent e) {
			Log.e("=============", "onDown============");
			x = e.getX();
			return true;
		}

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		int childCount = getChildCount();
		int specSizeWidth = MeasureSpec.getSize(widthMeasureSpec);
		int specSizeHeight = MeasureSpec.getSize(heightMeasureSpec);

		setMeasuredDimension(specSizeWidth, specSizeHeight);

		for (int i = 0; i < childCount; i++) {
			View child = getChildAt(i);
			this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
		}
	}

	class Item {
		private int cl;
		private int ct;
		private int cr;
		private int cb;

		public Item(int cl, int ct, int cr, int cb) {
			super();
			this.cl = cl;
			this.ct = ct;
			this.cr = cr;
			this.cb = cb;
		}

	}

	private Item[] item = new Item[5];
	private int temp = 0;

	private int centerDrawable = R.drawable.status_normal;
	
	public  void setDrawable(int drawable){
			this.centerDrawable=drawable;
			invalidate();
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		Paint mShadowPaint = new Paint(0);
		mShadowPaint.setColor(0xff101010);
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), centerDrawable);
		//bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter)
		int sWidth=getWidth();
		int finalWidth=sWidth/2;
		
		Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, finalWidth, finalWidth, true); 
		
		int width = mBitmap.getWidth();
		int height = mBitmap.getHeight();
		//canvas.drawBitmap(bitmap, null, new Rect(0, 0, 400, 400), null); 
		canvas.drawBitmap(mBitmap, (sWidth-finalWidth)/2, getHeight()-height, mShadowPaint);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {

		Log.d(TAG, "l:" + l + " t:" + t + " r:" + r + " b" + b);

		width = getWidth();
		int height = getHeight();
		Log.d(TAG, "onLayout width:" + width + " height:" + height);

		int childCount = getChildCount();

		int mLeft = 0;
		int mTop = 0;
		
		
		int mRight = getWidth();
		int mBottom = getHeight();
		int[] maxChildWidthHeight = getMaxChildWidthHeight();
		int a = 180 / childCount;

		// if (item[0] == null) {

		for (int i = 0; i < childCount; i++) {

			int position = i + temp;
			while (position >= childCount || position < 0) {
				if (position >= childCount) {
					position = position - childCount;
				} else if (position < 0) {
					position = childCount + position;
				}
			}

			View child = getChildAt(position);

			int[] childCenterXY;

			int offangle = 180 / (getChildCount()-1);

			childCenterXY = getChildCircleCenterXY(maxChildWidthHeight,
					offangle * (i), mLeft, mTop, mRight, mBottom);
			int x = childCenterXY[0];
			int y = childCenterXY[1];

			int cl = x - child.getMeasuredWidth() / 2;
			int ct = y - child.getMeasuredHeight() / 2;
			int cr = x + child.getMeasuredWidth() / 2;
			int cb = y + child.getMeasuredHeight() / 2;
			

			
			
			
			item[i] = new Item(cl, ct, cr, cb);
			
			child.layout(cl, ct, cr, cb);
		}
		// } else {
		// for (int i = 0; i < childCount; i++) {
		// View child = getChildAt(i);
		// if (i >= item.length) {
		// i = i - item.length;
		// }
		// Item aaa = item[i];
		// child.layout(aaa.cl, aaa.ct, aaa.cr, aaa.cb);
		// }
		//
		// }

	}

	private int[] getMaxChildWidthHeight() {
		int maxM = 0;
		int maxN = 0;

		int childCount = getChildCount();

		for (int i = 0; i < childCount; i++) {
			View child = getChildAt(i);

			maxM = Math.max(maxM, child.getMeasuredWidth());
			maxN = Math.max(maxN, child.getMeasuredHeight());
		}
		return new int[] { maxM, maxN };
	}

	private int[] getChildCircleCenterXY(int[] maxChildWH, float childAngle,
			int mLeft, int mTop, int mRight, int mBottom) {
		int x;
		int y;

		int temp = 120;

		int centerX = (mLeft + mRight) / 2;
		
		if(mRight>480&&mBottom>480){
			temp = 190;
		}
		if(mRight>480&&mBottom<480){
			temp = 130;
		}
		if(mRight<=480&&mBottom<370){
			temp=90;
		}
		
		Log.d(TAG, "=======================:" + mRight + " height:" + mBottom);
//		if(mRight<480&&mBottom)
		
		
		int centerY = (mTop + mBottom)-temp ;
		
//		if(mBottom>mRight){
//			centerY = mBottom-Math.abs(mBottom - mRight)-60;
//		}else{
//			centerY = mBottom-Math.abs(mBottom - mRight)+30;
//		}
		int left = mLeft + maxChildWH[0] / 2;
		int top = mTop + maxChildWH[1] / 2;
		int right = mRight - maxChildWH[0] / 2;
		// ȡmRight ������Ļ�Ŀ�����뾶
		int bottom = mRight - maxChildWH[1] / 2;

		double childSin = Math.sin(Math.toRadians(childAngle));// -1~1
		double childCos = Math.cos(Math.toRadians(childAngle));// -1~1

		// ˮƽ�뾶 ����
		int hR = Math.abs(right - left) / 2;
		// ��ֱ�뾶 ����
		int vR = Math.abs(bottom - top) / 2;

		x = (int) (centerX + hR * childCos);
		y = (int) (centerY - vR * childSin);

		return new int[] { x, y };
	}

}
